<%@ page import="com.alibaba.fastjson.JSON" %>
<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/message.jsp"%>
<%!

	// 创建通知
	String createNotification(Input input, Integer uid) {
		Message msg = new Message();
		Map parameters = input.getParameters();
		Integer type = input.getInt("type");
		Integer feedback = input.getInt("feedback");
		if (type == -9999) {
			msg.setErrcodeAndErrmsg(30041, "请传入type参数,通知类型");
			return msg.toJson();
		}
		if (feedback == -9999) {
			msg.setErrcodeAndErrmsg(30041, "请传入feedback参数,是否需要确认");
			return msg.toJson();
		}
		Notification notifiction = Notification.fromMap(parameters, Notification.class);
	    notifiction.setSender(uid + "");
		String imgs = JSONUtil.toJson(stringToList(input.getDesc("imgs", "")));
		String voices = JSONUtil.toJson(stringToList(input.getDesc("voices", "")));
		if (notifiction.getTitle() == null) {
			msg.setErrcodeAndErrmsg(30041, "缺少title参数");
			return msg.toJson();
		}	
		if (notifiction.getContent() == null) {
			msg.setErrcodeAndErrmsg(30041, "缺少content参数");
			return msg.toJson();
		}
		notifiction.setImgs(imgs);
		notifiction.setVoices(voices);
		List<String> receiverList = stringToList(input.getDesc("receiver", ""));
		String receiver = JSONUtil.toJson(receiverList);;
		notifiction.setReceiver(receiver);
		notifiction.setType(type);
		notifiction.setFeedback(feedback);
		Integer id = (Integer)DBTools.db("basedb").addItem(notifiction);
		if (id == null) {
			msg.setErrcodeAndErrmsg(30041, "添加失败");
			return msg.toJson();
		} else {
			List<NotificationConfirm> ncList = new ArrayList<NotificationConfirm>();
			for (Object obj:receiverList) {
				NotificationConfirm nc = NotificationConfirm.fromMap(parameters, NotificationConfirm.class);
				nc.setNid(id);
				nc.setUid(Tools.getInt(obj + ""));
				ncList.add(nc);
				Information info = new Information();
				if (type == 3) {
					info.setCategory(DOCTORWORD);
					info.setTitle("医护通知");
					info.setSumary("医护通知");
				} else if (type == 2) {
					info.setCategory(AFFAIRWORD);
					info.setTitle("事务通知");
					info.setSumary("事务通知");
				}
				info.setContent(notifiction.getTitle());
				info.setImgs(notifiction.getImgs());
				info.setOid(id + "");
				info.setHospital(input.getInt("hospital"));
				createMsg(info, Tools.getInt(obj + ""));
			}
			DBTools.db("basedb").addItem(ncList);
			msg.put("data", id);
		}
		return msg.toJson();
	}
%>
<%
	Tools.encryptPrint(out, createNotification(input, token.getUid()));
%>
